package com.ht.pms.core.service.impl;

import com.ht.pms.core.dao.DataImportDao;
import com.ht.pms.core.dto.Result;
import com.ht.pms.core.dto.upload.UpLoadTemp;
import com.ht.pms.core.dto.upload.UploadFile;
import com.ht.pms.core.service.ImportLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:喻尊龙
 * @date: 2018/4/24
 */
@Service("importLogService")
public class ImportLogServiceImpl implements ImportLogService {

    @Autowired
    private DataImportDao dataImportDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteImportData(String fileId, String batchCode) {
        dataImportDao.deleteUploadFile(fileId,batchCode);
        dataImportDao.deleteData(fileId,batchCode);
        return Result.buildSuccess();
    }

    @Override
    public Result getNewImportData() {
        UpLoadTemp upLoadTemp = dataImportDao.getNewUpLoadTemp();
        UploadFile uploadFile = null;
        Map<String,String> map = new HashMap<>();
        if(upLoadTemp != null){
            uploadFile  = dataImportDao.getNewUploadFile(upLoadTemp.getBatchCode());
        }
        if(uploadFile != null){
            map.put("fileId",uploadFile.getFileId()+"");
            map.put("templateCode",upLoadTemp.getTemplateCode());
        }
        return Result.buildSuccess(map);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result checkDate(String fileId, String batchCode, String templateCode) {
        dataImportDao.updataRecordCheckState(fileId,batchCode,templateCode);
        dataImportDao.updataCheckState(fileId,batchCode);
        return Result.buildSuccess();
    }
}
